﻿/*-----------------------------------------------------------------------
 * <copyright file="Converter.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Li Dawei&Zhang Jun</author>
 * <summary>
 *          数字格式转换 如123,345.00
 *          作成日： 08/04/2010 
 *
 * </summary>
 *-----------------------------------------------------------------------
 */

namespace Orid.Management.Statistics.OperatorUnitMangement.Common.SytleSet
{
    using System;
    using System.Globalization;
    using System.Windows.Data;

    /// <summary>
    /// 数字格式转换 如123,345.00
    /// </summary>
    public class DoubleConverter : IValueConverter
    {
        /// <summary>
        /// 正向转换器。将值从数据源传给绑定目标时，数据绑定引擎会调用此方法
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value,
                          Type targetType,
                          object parameter,
                          CultureInfo culture)
        {
            //Double d = (double )value;
            //return d.ToString("N2");
            //上面这样写如果传入的值是string类型 不能转换
            try
            {
                Double d = double.Parse(value.ToString());
                return d.ToString("#,##0.00");//#,##0
            }
            catch (Exception ex)
            {

            }
            return value;
        }

        /// <summary>
        /// 反向转换器。将值从绑定目标传给数据源时，数据绑定引擎会调用此方法
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value,
                                Type targetType,
                                object parameter,
                                CultureInfo culture)
        {
            string strValue = value.ToString();
            Double result;
            if (Double.TryParse(strValue, out result))
            {
                return result;
            }
            return value;
        }

    }
}
